package org.iabako.client.navigation;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.RootPanel;
import org.iabako.client.config.page.PageEnum;
import org.iabako.client.tools.IabakoAsyncCallback;
import org.iabako.client.tools.Messages;
import org.iabako.client.ui.layout.widget.dialogbox.IabakoDialogBox;
import org.iabako.client.ui.layout.widget.pricetable.PriceTable;
import org.iabako.client.ui.page.importdata.ImportClient;
import org.iabako.shared.entity.user.UserPayPalToken;
import org.iabako.shared.iservice.user.SignUpServiceAsync;
import org.iabako.shared.iservice.user.UserServiceAsync;
import org.iabako.shared.tools.GenericTools;

/**
 * Created by jose on 16/12/14.
 */
public class URLByPassAccess {

    public static void urlByPassAccess(final boolean publicAccess) {
        String urlHash = Window.Location.getHash();

        if (verifyHiddenPages(urlHash)) {
            return;
        }

        if (verifyPriceTablePage(urlHash)) {
            return;
        }

        boolean result = verifyPaymentURLPages(urlHash, publicAccess);

        if (publicAccess) {
            NavigationController.getInstance().setHomeURL();
        } else {
            NavigationController.getInstance().goTo(PageEnum.IabakoHome);
        }

        if (result) {
            return;
        }
    }

    private static boolean verifyHiddenPages(String urlHash) {
        if (urlHash.contains("#ImportClient")) {
            RootPanel.get().clear();
            RootPanel.get().add(new ImportClient());
            return true;
        }
        return false;
    }

    private static boolean verifyPriceTablePage(String urlHash) {
        if (urlHash.contains("#PricesTable")) {
            RootPanel.get().clear();
            RootPanel.get().add(new PriceTable(false));
            return true;
        }
        return false;
    }

    private static boolean verifyPaymentURLPages(String urlHash, boolean publicAccess) {
        if (urlHash.contains("#PaymentCancel")) {
            NavigationController.getInstance().show(new IabakoDialogBox("payment_cancel_popup_title", "payment_cancel_popup_text"));
            return true;
        }
        if (urlHash.contains("#PaymentFail")) {
            NavigationController.getInstance().show(new IabakoDialogBox("payment_fail_popup_title", "payment_fail_popup_text"));
            return true;
        }
        if (urlHash.contains("#PaymentError")) {
            NavigationController.getInstance().show(new IabakoDialogBox("payment_error_popup_title", "payment_error_popup_text"));
            return true;
        }
        if (urlHash.contains("#PaymentSuccess")) {

            String[] params = NavigationController.getInstance().getParams();
            if (params != null && params.length == 1) {

                if (publicAccess) {
                    SignUpServiceAsync.Util.getInstance().getUserPayPalTokenPayedOrToVerify(params[0], new IabakoAsyncCallback<UserPayPalToken>() {
                        public void onSuccess(UserPayPalToken result) {
                            showPaymentDialogBox(result, true);
                            super.onSuccess(result);
                        }
                    });
                } else {
                    UserServiceAsync.Util.getInstance().getUserPayPalTokenPayedOrToVerify(params[0], new IabakoAsyncCallback<UserPayPalToken>() {
                        public void onSuccess(UserPayPalToken result) {
                            showPaymentDialogBox(result, false);
                            super.onSuccess(result);
                        }
                    });
                }
            } else {
                NavigationController.getInstance().show(new IabakoDialogBox("payment_fail_popup_title", "payment_fail_popup_text"));
            }
            return true;
        }
        return false;
    }

    private static void showPaymentDialogBox(UserPayPalToken userPayPalToken, boolean publicAccess) {
        if (userPayPalToken != null) {

            if (userPayPalToken.isPayed()) {

                NavigationController.getInstance().show(new IabakoDialogBox(
                        Messages.getLabel("payment_success_popup_title", userPayPalToken.getPayer().getFirstName()),
                        Messages.getLabel(publicAccess ? "payment_success_popup_text_not_logged" : "payment_success_popup_text",
                                userPayPalToken.getPayer().getEnterprise().getName(),
                                GenericTools.formatAmount(userPayPalToken.getAmount() + ""),
                                userPayPalToken.getPayPalPayerEmail()), "paypalsuccessicon"));

            } else if (userPayPalToken.isToVerify()) {

                NavigationController.getInstance().show(new IabakoDialogBox(
                        Messages.getLabel("payment_success_popup_title", userPayPalToken.getPayer().getFirstName()),
                        Messages.getLabel(publicAccess ? "payment_to_verify_popup_text_not_logged" : "payment_to_verify_popup_text",
                                GenericTools.formatAmount(userPayPalToken.getAmount() + ""),
                                userPayPalToken.getPayPalPayerEmail()), "paypalsuccessicon"));

            }
        } else {
            NavigationController.getInstance().show(new IabakoDialogBox("payment_fail_popup_title", "payment_fail_popup_text"));
        }
    }
}
